package com.sdy.mvc.interceptor;

import com.sdy.common.utils.EncodeUtil;
import com.sdy.common.utils.RandomUtil;
import com.sdy.mvc.config.SdyMvcConfig;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RemoteApiRequestPreInterceptor implements RequestInterceptor {
    @Value("${inner_token:}")
    private String innerToken;
    
    @Override
    public void apply(RequestTemplate template) {
        String randomStr = RandomUtil.produceString(6);
        String currentTimeStr = String.valueOf(System.currentTimeMillis());
        String encodedStr = EncodeUtil.sha256(innerToken.concat("#").concat(randomStr).concat("#").concat(currentTimeStr))
                .concat("#").concat(randomStr).concat("#").concat(currentTimeStr);
        template.header(SdyMvcConfig.SERVICE_REQ_TOKEN_ENC, encodedStr);
        log.info("Remote api invoke! Url:{}", template.path());
    }
}
